package com.qtjf.qtmoneyservice.view;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Html;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.activity.ShenFenActivity;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.widget.ShenFenDialog;

import java.io.File;

/**
 * Created by Administrator on 2017/8/1 0001.
 */

public class ShenFenRenGongView {

    Context mContext;
    View mView;
    TextView tipText;
    FrameLayout layout1;
    ImageView layout2;
    TextView submitBt;
    ImageView img1;

    public ShenFenRenGongView(Context mContext) {
        this.mContext = mContext;
    }

    public View getView()
    {
        mView=View.inflate(mContext, R.layout.shenfen_rengong_view,null);
        initView();
        initData();
        initEvent();
        return mView;
    }

    private void initView() {
        tipText= (TextView) mView.findViewById(R.id.tip_text);
        tipText.setText(Html.fromHtml("<font color='#666666'>温馨提示:</font> 材料审核需要1个工作日，如有疑问，请点击右上角联系客服。"));
        layout1= (FrameLayout) mView.findViewById(R.id.layout1);
        layout2= (ImageView) mView.findViewById(R.id.layout2);
        img1= (ImageView) mView.findViewById(R.id.img1);

        LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) layout1.getLayoutParams();
        params.height= (int)(((Constant.getSCREENWIDTH()-180*Constant.getSCALE())/(219.0/144.0)));
        layout1.setLayoutParams(params);

        LinearLayout.LayoutParams params1= (LinearLayout.LayoutParams) layout2.getLayoutParams();
        params1.height= (int)(((Constant.getSCREENWIDTH()-180*Constant.getSCALE())/(219.0/144.0)));
        layout2.setLayoutParams(params1);

        submitBt= (TextView) mView.findViewById(R.id.submit_bt);
    }

    private void initData() {
    }

    private void initEvent() {

        submitBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ShenFenDialog dialog=new ShenFenDialog(mContext,R.style.myTransparent,null);
                dialog.show();
            }
        });


        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String status=Environment.getExternalStorageState();
                if(status.equals(Environment.MEDIA_MOUNTED))
                {
                    try {
                        File dir=new File(Environment.getExternalStorageDirectory() + "/"+"imgs");
                        if(!dir.exists())dir.mkdirs();

                        Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        File f=new File(dir, "imgs1.jpg");//localTempImgDir和localTempImageFileName是自己定义的名字
                        Uri u=Uri.fromFile(f);
                        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
                        ((ShenFenActivity)mContext).startActivityForResult(intent, 1);
                    } catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
                        Toast.makeText(mContext, "没有找到储存目录",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(mContext, "没有储存卡",Toast.LENGTH_LONG).show();
                }

            }
        });

        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String status=Environment.getExternalStorageState();
                if(status.equals(Environment.MEDIA_MOUNTED))
                {
                    try {
                        File dir=new File(Environment.getExternalStorageDirectory() + "/"+"imgs");
                        if(!dir.exists())dir.mkdirs();

                        Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        File f=new File(dir, "imgs1.jpg");//localTempImgDir和localTempImageFileName是自己定义的名字
                        Uri u=Uri.fromFile(f);
                        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
                        ((ShenFenActivity)mContext).startActivityForResult(intent, 2);
                    } catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
                        Toast.makeText(mContext, "没有找到储存目录",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(mContext, "没有储存卡",Toast.LENGTH_LONG).show();
                }
        }
        });
    }

    public void setImage1(Bitmap bitmap)
    {
        img1.setImageBitmap(bitmap);
    }

    public void setImage2(Bitmap bitmap)
    {
        layout2.setImageBitmap(bitmap);
    }

}
